package me.jessyan.mvparms.demo.mvp.presenter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.app.utils.RxUtils;
import me.jessyan.mvparms.demo.data.entity.MessageListEntity;
import me.jessyan.mvparms.demo.data.entity.TopResponse;
import me.jessyan.mvparms.demo.data.entity.UploadImgEntity;
import me.jessyan.mvparms.demo.mvp.contract.MessageListContract;
import me.jessyan.mvparms.demo.mvp.ui.adapter.MessageListAdapter;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;

/**
 * @Author: 海洋
 * @Date: 2018/8/23
 * @Desc:
 */
public class MessageListPresenter extends BasePresenter<MessageListContract.Model, MessageListContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    List<MessageListEntity> mMessageList;
    @Inject
    MessageListAdapter mMessageListAdapter;

    @Inject
    public MessageListPresenter(MessageListContract.Model model, MessageListContract.View rootView) {
        super(model, rootView);
    }

    public void getMessageList(String type, String page, String limit, boolean isInit, boolean isPullToRefresh) {
//        RxUtils.<TopResponse<List<MessageListEntity>>>applySchedulers(mRootView,
//                mRootView.getActivity().getString(R.string.is_loading))
//                .apply(mModel.getMessageList(type, page, limit))
//                .subscribe(new ErrorHandleSubscriber<TopResponse<List<MessageListEntity>>>(mErrorHandler) {
//                    @Override
//                    public void onNext(TopResponse<List<MessageListEntity>> response) {
//                        if (isPullToRefresh) mMessageList.clear();
//                        if (response.getData() != null && response.getData().size() > 0) {
//                            mMessageList.addAll(response.getData());
//                            mMessageListAdapter.notifyDataSetChanged();
//                            if (response.getData().size() < mRootView.getLimit()) {
//                                mRootView.noMore();
//                            }
//                        }
//                    }
//                });
        mModel.getMessageList(type, page, limit)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    if (isInit)
                        mRootView.showLoading(mRootView.getActivity().getString(R.string.is_loading));//显示进度条
                    else if (isPullToRefresh)
                        mRootView.startPullToRefresh();
                    else
                        mRootView.startLoadMore();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (isInit)
                        mRootView.hideLoading();//隐藏进度条
                    else if (isPullToRefresh)
                        mRootView.endPullToRefresh();
                    else
                        mRootView.endLoadMore();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<TopResponse<List<MessageListEntity>>>(mErrorHandler) {
                    @Override
                    public void onNext(TopResponse<List<MessageListEntity>> response) {
                        if (response.getStatus().equals("200")){
                            if (isPullToRefresh) mMessageList.clear();
                            if (response.getData() != null && response.getData().size() > 0) {
                                mMessageList.addAll(response.getData());
                                mMessageListAdapter.notifyDataSetChanged();
                                if (response.getData().size() < mRootView.getLimit()) {
                                    mRootView.noMore();
                                }
                            }
                        }else {
                            mRootView.showMessage(mRootView.getActivity().getString(R.string.status_not_200));
                        }

                    }
                });
    }
}
